package com.example.edu_realtime_ads.service.impl;

import com.example.edu_realtime_ads.bean.CategoryWithOrder;
import com.example.edu_realtime_ads.bean.CourseWithOrder;
import com.example.edu_realtime_ads.bean.ReviewCourse;
import com.example.edu_realtime_ads.bean.SubjectwithOrder;
import com.example.edu_realtime_ads.mapper.OrderMapper;
import com.example.edu_realtime_ads.service.OrderService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @author MengX
 * @create 2023/3/28 13:50:01
 */
@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    OrderMapper orderMapper;
    @Override
    public String getSubjectJson(Integer date) {
        List<SubjectwithOrder> subjectWithOrderTable = orderMapper.getSubjectWithOrderTable(date);
        if (subjectWithOrderTable == null){
            return "";
        }
        ArrayList<String> subject = new ArrayList<>();
        ArrayList<Long> orderUc = new ArrayList<>();
        ArrayList<Long> orderCt = new ArrayList<>();
        ArrayList<BigDecimal> orderAmount = new ArrayList<>();

        for (SubjectwithOrder subjectwithOrder : subjectWithOrderTable) {
            subject.add(subjectwithOrder.getSubjectName());
            orderUc.add(subjectwithOrder.getOrderUc());
            orderAmount.add(subjectwithOrder.getOrderAmount());
            orderCt.add(subjectwithOrder.getOrderct());
        }

        StringBuilder jsonStr = new StringBuilder();
        String joinSub = StringUtils.join(subject, "\",\"");
        String joinOderUc = StringUtils.join(orderUc, ",");
        String joinOderCt = StringUtils.join(orderCt,",");
        String joinAmount = StringUtils.join(orderAmount, ",");
        jsonStr.append("{\n" +
                "  \"status\": 0,\n" +
                "  \"msg\": \"\",\n" +
                "  \"data\": {\n" +
                "    \"categories\": [ \"")
                .append(joinSub).append("\" ],")
                .append("\"series\": [\n" +
                        "      {\n" +
                        "        \"name\": \"下单人数\",\n" +
                        "        \"data\": [")
                .append(joinOderUc).append("]\n" +
                        "      },\n" +
                        "      {\n" +
                        "        \"name\": \"下单次数\",\n" +
                        "        \"data\": [")
                .append(joinOderCt).append(" ]\n" +
                        "      },\n" +
                        "      {\n" +
                        "        \"name\": \"下单金额\",\n" +
                        "        \"data\": [")
                .append(joinAmount).append(" ]\n" +
                        "      }\n" +
                        "    ]\n" +
                        "  }\n" +
                        "}");

        return jsonStr.toString();
    }

    @Override
    public String getCategoryJson(Integer date) {
        List<CategoryWithOrder> categoryWithOrderTable = orderMapper.getCategoryWithOrderTable(date);
        if (categoryWithOrderTable == null){
            return "";
        }
        StringBuilder categoryJson = new StringBuilder();
        categoryJson.append("{\n" +
                "  \"status\": 0,\n" +
                "  \"msg\": \"\",\n" +
                "  \"data\": [");
        for (CategoryWithOrder categoryWithOrder : categoryWithOrderTable) {
            Long orderUc = categoryWithOrder.getOrderUc();
            Long orderct = categoryWithOrder.getOrderct();
            BigDecimal orderAmount = categoryWithOrder.getOrderAmount();
            categoryJson.append(" {\n" + "      \"name\": \"下单人数\",\n" + "      \"value\": ").append(orderUc).append("\n").append("    },\n").append("    {\n").append("      \"name\": \"下单次数\",\n").append("      \"value\": ").append(orderct).append("\n").append("    },\n").append("    {\n").append("      \"name\": \"下单金额\",\n").append("      \"value\": ").append(orderAmount).append("\n").append("    }\n").append("  ]\n").append("}");
        }

        return categoryJson.toString();
    }

    @Override
    public String getCourseJson(Integer date) {
        List<CourseWithOrder> courseJson = orderMapper.getCourseJson(date);

        if (courseJson == null){
            return "";
        }
        ArrayList<String> course = new ArrayList<>();
        ArrayList<Long> orderUc = new ArrayList<>();
        ArrayList<BigDecimal> orderAmount = new ArrayList<>();
        for (CourseWithOrder courseWithOrder : courseJson) {
            course.add(courseWithOrder.getCourseName());
            orderUc.add(courseWithOrder.getOrderUc());
            orderAmount.add(courseWithOrder.getOrderAmount());
        }
        StringBuilder jsonStr = new StringBuilder();
        String join = StringUtils.join(course, "--");
        String replace = join.replace("\\", "'");
        String[] split = replace.split("--");
        String joinCourse = StringUtils.join(split, "\",\"");
        String joinOrderUc = StringUtils.join(orderUc, ",");
        String joinAmount = StringUtils.join(orderAmount, ",");
        jsonStr.append("{\n" +
                "  \"status\": 0,\n" +
                "  \"msg\": \"\",\n" +
                "  \"data\": {\n" +
                "    \"categories\": [\n" +
                "      \"")
                .append(joinCourse).append("\"\n" +
                        "    ],\n" +
                        "    \"series\": [\n" +
                        "      {\n" +
                        "        \"name\": \"下单人数\",\n" +
                        "        \"data\": [")
                .append(joinOrderUc).append("]\n" +
                        "      },\n" +
                        "      {\n" +
                        "        \"name\": \"下单金额\",\n" +
                        "        \"data\": [")
                .append(joinAmount).append(" ]\n" +
                        "      }\n" +
                        "    ]\n" +
                        "  }\n" +
                        "}");

        return jsonStr.toString();
    }

    @Override
    public String getReviewJson(Integer date) {
        List<ReviewCourse> review = orderMapper.getReview(date);
        if (review == null){
            return "";
        }
        StringBuilder jsonStr = new StringBuilder();
        ArrayList<String> course = new ArrayList<>();
        ArrayList<Long> reviewUc = new ArrayList<>();
        ArrayList<BigDecimal> startAvg = new ArrayList<>();
        ArrayList<BigDecimal> positive = new ArrayList<>();

        for (ReviewCourse reviewCourse : review) {
            course.add(reviewCourse.getCourseName());
            reviewUc.add(reviewCourse.getUserCt());
            startAvg.add(reviewCourse.getStarAvg());
            positive.add(reviewCourse.getPositiveReview());
        }
        String join = StringUtils.join(course, "--");
        String replace = join.replace("\\", "'");
        String[] split = replace.split("--");
        String joinCourse = StringUtils.join(split, "\",\"");
        String joinReview = StringUtils.join(reviewUc, ",");
        String joinStar = StringUtils.join(startAvg, ",");
        String joinPos = StringUtils.join(positive, ",");

        jsonStr.append("{\n" +
                "  \"status\": 0,\n" +
                "  \"msg\": \"\",\n" +
                "  \"data\": {\n" +
                "    \"series\": [\n" +
                "      {\n" +
                "        \"name\": \"用户平均评分\",\n" +
                "        \"data\": [")
                .append(joinStar).append("  ],\n" +
                        "        \"type\": \"bar\",\n" +
                        "        \"yAxisIndex\": 0\n" +
                        "      },\n" +
                        "      {\n" +
                        "        \"name\": \"评价用户数\",\n" +
                        "        \"data\": [")
                .append(joinReview).append(" ],\n" +
                        "        \"type\": \"bar\",\n" +
                        "        \"yAxisIndex\": 0\n" +
                        "      },\n" +
                        "      {\n" +
                        "        \"name\": \"好评率\",\n" +
                        "        \"data\": [")
                .append(joinPos).append("],\n" +
                        "        \"type\": \"line\",\n" +
                        "        \"yAxisIndex\": 1\n" +
                        "      }\n" +
                        "    ],\n" +
                        "    \"categories\": [\n" +
                        "      \"")
                .append(joinCourse).append("\"\n" +
                        "    ]\n" +
                        "  }\n" +
                        "}");

        return jsonStr.toString();
    }

}
